package com.kuhyygy.activity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kuhyygy.R;
import com.kuhyygy.file.FileService;
import com.kuhyygy.uitl.ActivityCollector;

public class NoteBookActivity extends BaseActivity {

	private String tag = "NoteBookActivity";
	
	private EditText etName;
	private EditText etCont;
	private FileService fs;
	private TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notebook);
		fs = new FileService();
		Button saveBtn = (Button) this.findViewById(R.id.saveContent);
		Button readBtn = (Button) this.findViewById(R.id.readContent);
		etName = (EditText) this.findViewById(R.id.fileName);
		etCont = (EditText) this.findViewById(R.id.fileContent);
		tv = (TextView) this.findViewById(R.id.contentView);
		//saveBtn.setOnClickListener(clickLst);
		//readBtn.setOnClickListener(clickLst);
	}
	
	private OnClickListener clickLst = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.saveContent: {
				Log.i(tag, etName.getText().toString());
				String fileName = etName.getText().toString();
				String content = etCont.getText().toString();
				try {
					fs.save(NoteBookActivity.this.openFileOutput(fileName, Context.MODE_APPEND), content);
					Toast.makeText(NoteBookActivity.this, "内容保存成功", Toast.LENGTH_SHORT).show();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			}
			case R.id.readContent: {
				String fileName = etName.getText().toString();
				try {
					FileInputStream fis = NoteBookActivity.this.openFileInput(fileName);
					String content = fs.read(fis);
					tv.setText(content);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			default:
				break;
			}
		}
		
	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.personalSetting:
			break;
		case R.id.exit:
			ActivityCollector.removeAll();
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
